#coding:utf-8
from lovewith.card.models import Card, CardPost, CardInvite


class CardApi:
    def __init__(self):
        pass

    #验证用户是否已经创建了请柬
    @staticmethod
    def check_data(user_id):
        card_filter_data = Card.objects.filter(user_id=int(user_id))
        if card_filter_data.exists():
            return card_filter_data[0]
        return None

    #验证请柬是否存在
    @staticmethod
    def check_card(card_id):
        card_filter_data = Card.objects.filter(id=int(card_id))
        if card_filter_data.exists():
            return card_filter_data[0]
        return None

    #增加TODO 统一其它model的更新方法
    def update(self, **kwargs):
        card_data = self.check_data(kwargs['user'].id)
        if not card_data:
            new_card = Card(**kwargs)
            new_card.save()

            return new_card.id
        else:
            for key, value in kwargs.iteritems():
                setattr(card_data, key, value)
            card_data.save()

            return card_data.id

    #获取请柬内页数据
    @staticmethod
    def get_postdata_by_card(card_id):
        post_data = []
        #限制只显示20条数据
        post_filter_data = CardPost.objects.filter(card_id=int(card_id)).order_by('-id')
        for post in post_filter_data:
            post_data.append({
                'id': post.id,
                'title': post.title,
                'content': post.content,
                'media': post.media
            })

        return post_data

    #验证请柬内容是否属于用户
    @staticmethod
    def check_post_data(post_id, user_id):
        post_filter_data = CardPost.objects.filter(id=int(post_id), user_id=int(user_id))
        if post_filter_data.exists():
            return post_filter_data[0]
        else:
            return None

    #增加文字内页
    @staticmethod
    def add_post_data(**kwargs):
        new_post = CardPost(**kwargs)
        new_post.save()

        return new_post


    #判断被邀请用户是否已添加
    @staticmethod
    def check_invite_user(card_id, user_id=None, invite_type=None):
        if card_id:
            #不知道索引id时候用用户id，类型查询
            if user_id and invite_type:
                invite_filter_data = CardInvite.objects.filter(
                    user_id=user_id,
                    invite_type=invite_type,
                    card_id=int(card_id)
                )
                if invite_filter_data.exists():
                    return invite_filter_data[0]

        return None


    #更新邀请用户
    @staticmethod
    def update_invite(**kwargs):
        new_invite = CardInvite(**kwargs)
        new_invite.save()

        return new_invite


    #获取邀请的用户列表
    @staticmethod
    def get_invite_user(card_id):
        return CardInvite.objects.filter(card_id=int(card_id)).order_by('-id')

    #判断是否被邀请
    @staticmethod
    def have_invited(card_id, email=None, app_uid=None):
        invited_user_data = None
        if email:
            invited_user_data = CardInvite.objects.filter(card_id=int(card_id), user_email=email, invite_type='email')
        if app_uid:
            invited_user_data = CardInvite.objects.filter(card_id=int(card_id), user_id=app_uid)

        if invited_user_data and invited_user_data.exists():
            return invited_user_data[0]

        return False